import {Router, Get, Body, Parse, Post, Query} from "../decoration/decorators"
import {Inject} from "../IOC/Inject"
import {ContactService} from "../Service/ContactService";
import { Context } from "koa";
import {errorMsg,successMsg} from "../utils/errorMsg"
import {ContactEntity} from "../models/Contact.entity";

@Router('/contact')
export default class ContactController {

    @Inject public contactService:ContactService

    @Post('/create')
    async create(@Body() all:any,ctx:Context):Promise<void> {
        const status = await this.contactService.createContact(ctx,all)
        if(!status) {
            ctx.response.status = 500
            errorMsg.Instance().setErrorMsg("留言失败")
        } else {
            successMsg.Instance().setSuccessMsg("留言成功")
        }
    }

    @Get('/')
    async get(@Query() all:Object):Promise<Boolean | ContactEntity[]> {
        const data = await this.contactService.findContact()
        if(typeof data === 'boolean') {
            errorMsg.Instance().setErrorMsg("查询失败")
        } else {
            successMsg.Instance().setSuccessMsg("查询成功")
            return data
        }
    }

}
